#include "fxcc/core/BaseImporter.h"

std::string BaseImporter::m_LibraryPath = { "Library" };

std::string BaseImporter::m_AssertPath = { "Assert" };

BaseImporter::BaseImporter()
	:m_Name("baseImporter")
{
}

bool BaseImporter::HasImportFile(const std::string& p) const
{
	auto metaFile = GetMetaFile(p);
	if (!fs::exists(metaFile))
	{
		return false;
	};

	static fxcc::yml::Serializer serializer;
	MetaIndex metaIndex;
	bool flag = serializer.DecodePath<MetaIndex>(metaFile, metaIndex);

	if (metaIndex.m_FilePath != p || metaIndex.m_LastUpdateTime != fxcc::FileUtil::getLastWriteTimestamp(p))
	{
		return false;
	}
	return true;

}

bool BaseImporter::CreateImportMeta(const std::string& path) const
{
	ztclog::info("[%s] import %s", m_Name.c_str(), path.c_str());

	MetaIndex metaIndex;
	metaIndex.m_FilePath = path;
	metaIndex.m_LastUpdateTime = fxcc::FileUtil::getLastWriteTimestamp(path);

	static fxcc::yml::Serializer serializer;
	return serializer.EncodePath<MetaIndex>(GetMetaFile(path), metaIndex);
}

void BaseImporter::ImportDir(const fxcc::Dir& dir, bool inc)
{
	for (const auto& childDir : dir.m_Dirs)
	{
		ImportDir(*childDir);
	}

	for (const auto& file : dir.m_Files)
	{
		if (inc && HasImportFile(file->m_RelativePath))
		{
			ztclog::info("[%s] has import the file %s", m_Name.c_str(), file->m_RelativePath.c_str());
		}
		else
		{
			if (ImportFile(*file))
			{
				CreateImportMeta(file->m_RelativePath);
			}
		}
	}
}
